package com.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.blog.filter.SensitiveFilter;
import com.blog.pojo.Message;
import com.blog.pojo.User;
import com.blog.service.MessageService;
import com.blog.utils.IpUtils;
import com.blog.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-05-31 14:48
 * FileName: MessageController
 * Description: 留言控制层
 */
@RestController
@RequestMapping("message")
@Slf4j
public class MessageController extends BaseController{
    @Resource
    private MessageService messageService;

    @GetMapping("/getMessageList")
    public String selectMessageList(){
        JSONObject object=new JSONObject();
        List<Message> messageList= messageService.getMessageList();
        object.put("MessageList",messageList);
        object.put("flag",messageList!=null);
        return object.toJSONString();
    }
    @PostMapping("/add/{uid}")
    public String add(HttpServletRequest request,@RequestBody Message message,@PathVariable("uid") Long uid){
        JSONObject object=new JSONObject();
        message.setMid(UUIDUtils.getNumber());
        //判断用户是否是登录状态 如果不是就发送游客信息
        if(checkUser(uid)!=null){
            message.setUser(checkUser(uid));
        }else{
            message.setUser(new User().setUid(1L));
        }
        String messageContent= SensitiveFilter.filter(message.getMessageContent().trim());
        //判断是否包含敏感词
        if(messageContent.equals(message.getMessageContent())){
            object.put("flag2",false);
        }else{
            object.put("flag2",true);
        }
        if(messageService.add(message)){
            object.put("messageContent",messageContent);
            object.put("flag",true);
            return object.toJSONString();
        }
        object.put("flag",false);
        object.put("message","留言失败");
        return object.toJSONString();
    }
}
